package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.kgd.news.entity.SysDict;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtilsWx;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/6/20
 * @Time: 下午4:18
 */
public class DictController extends BaseController {

    @Override
    public void list() {
        String group = getPara("group");
        String value = getPara("value");
        String sql = "from sys_dict where 1 = 1";
        String type = "dict";
        if (!StringUtilsWx.isEmpty(group)) {
            sql += " and d_group like '%" + group + "%'";
            type += group;
        }
        if (!StringUtilsWx.isEmpty(value)) {
            sql += " and d_value = " + value + "";
            type += value;
        }
        sql += " order by id desc";
        Page<SysDict> data = SysDict.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        render("dict_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("did");
        SysDict dict = SysDict.dao.findById(id);
        setAttr("dict", dict);
        render("dict_edit.html");
    }

    @Override
    public void save() {
        SysDict sd = getModel(SysDict.class, "dict");
        if(sd != null && sd.getId() != null){
            sd.update();
        }else{
            sd.save();
        }
        list();

    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from sys_dict where id in (" + ids + ")");

        list();
    }
}
